#include "button.h"
#include "systimer.h"
#include <stdio.h>
#include "system.h"

static Button button[BUTTON_NUM] = {
    {Btn1, Release, 5, 0, 20, 3000,  0, 0, GPIOA, GPIO_PIN_5},
    {Btn2, Release, 5, 0, 20, 65535,  0, 0, GPIOA, GPIO_PIN_6},
    {Btn3, Release, 5, 0, 20, 65535,  0, 0, GPIOA, GPIO_PIN_7},
    {Btn4, Release, 5, 0, 20, 65535,  0, 0, GPIOB, GPIO_PIN_0},
    // {Btn5, Release, 5, 0, 20, 65535,  0, 0, GPIOB, GPIO_PIN_1},
    // {Btn6, Release, 5, 0, 20, 65535,  0, 0, GPIOB, GPIO_PIN_2}
    // {Btn7, Release, 5, 0, 20, 65535,  0, 0, GPIOD, GPIO_PIN_0}
    // {Btn8, Release, 5, 0, 20, 65535,  0, 0, GPIOD, GPIO_PIN_1}
};

void button_init(void)  
{
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOA);
    gpio_init(button[Btn1].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn1].pin);
    gpio_init(button[Btn2].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn2].pin);
    gpio_init(button[Btn3].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn3].pin);
    gpio_init(button[Btn4].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn4].pin);
    // gpio_init(button[Btn5].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn5].pin);
    // gpio_init(button[Btn6].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn6].pin);
    // gpio_init(button[Btn7].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn7].pin);
    // gpio_init(button[Btn8].gpio_periph, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, button[Btn8].pin);
}

void button_scan(void)
{
    for(uint8_t i = 0; i < BUTTON_NUM; i++)
    {
        /* 未生成键值 */
        if(button[i].flag == 0)
        {
            /* 按键按下 */
            if(gpio_input_bit_get(button[i].gpio_periph, button[i].pin) == 0)
            {
                button[i].pressTimeSum += button[i].pressTimeOnce;
                if(button[i].pressTimeSum >= button[i].longPressTimeMin)
                {
                    button[i].state = LongPressed;
                    button[i].flag = 1;
                }
            }
            /* 按键松开 */
            else
            {
                if(button[i].pressTimeSum >= button[i].pressTimeMin)
                {
                    button[i].state = Pressed;
                    button[i].flag = 1;
                }
                else
                {
                    // button[i].state = Release;
                    button[i].pressTimeSum = 0;
                    button[i].flag = 0;
                }
            }
        }
        /* 已生成键值 */
        else
        {
            /* 按键松开 */
            if(gpio_input_bit_get(button[i].gpio_periph, button[i].pin) != 0)
            {
                // button[i].state = Release;
                button[i].pressTimeSum = 0;
                button[i].flag = 0;
            }
        }
    }
}

BtnState button_get_state(uint8_t id)
{
    BtnState state = button[id].state;
    if(state != Release)
    {
        button[id].state = Release;
    }
    return state;
}

void button_self_test(void)
{
    BtnState btnState;
    btnState = button_get_state(Btn1);
    if(btnState != Release)
    {
        if(btnState == Pressed)
        {
            debug("btn1 pressed\r\n");
        }
        else if(btnState == LongPressed)
        {
            debug("btn1 longPressed\r\n");
        }
    }
    btnState = button_get_state(Btn2);
    if(btnState != Release)
    {
        if(btnState == Pressed)
        {
            debug("Btn2 pressed\r\n");
        }
        else if(btnState == LongPressed)
        {
            debug("Btn2 longPressed\r\n");
        }
    }
    btnState = button_get_state(Btn3);
    if(btnState != Release)
    {
        if(btnState == Pressed)
        {
            debug("Btn3 pressed\r\n");
        }
        else if(btnState == LongPressed)
        {
            debug("Btn3 longPressed\r\n");
        }
    }
    btnState = button_get_state(Btn4);
    if(btnState != Release)
    {
        if(btnState == Pressed)
        {
            debug("Btn4 pressed\r\n");
        }
        else if(btnState == LongPressed)
        {
            debug("Btn4 longPressed\r\n");
        }
    }
    // btnState = button_get_state(Btn5);
    // if(btnState != Release)
    // {
    //     if(btnState == Pressed)
    //     {
    //         debug("btnUp pressed\r\n");
    //     }
    //     else if(btnState == LongPressed)
    //     {
    //         debug("btnUp longPressed\r\n");
    //     }
    // }
    // btnState = button_get_state(Btn_Page);
    // if(btnState != Release)
    // {
    //     if(btnState == Pressed)
    //     {
    //         debug("btnPage pressed\r\n");
    //     }
    //     else if(btnState == LongPressed)
    //     {
    //         debug("btnPage longPressed\r\n");
    //     }
    // }
    
}
